package com.mystnihon.freeremote.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.ViewFlipper;

import com.mystnihon.freeremote.R;

public class GeneralFragment extends DefaultSettingsFragment {

	private static final String FR_MYSTNIHON_REMOTE_CHECKED_GENERAL_BUTTON = "fr.mystnihon.remote.checkedGeneralButton";
	private ViewFlipper mViewFlipper;
	private RadioGroup mRadioGroup;

	public static GeneralFragment newInstance() {

		GeneralFragment fragment = new GeneralFragment();

		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_general, null);
		mViewFlipper = (ViewFlipper) v.findViewById(R.id.fr_vf_remote_channel);
		mRadioGroup = (RadioGroup) v.findViewById(R.id.fr_rg_remote_channel);
		initViews(savedInstanceState);
		return v;
	}

	private void initViews(Bundle savedInstanceState) {
		if (mViewFlipper != null && mRadioGroup != null) {
			mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					switch (checkedId) {
						case R.id.fr_rb_channel:
							mViewFlipper.setDisplayedChild(1);
							break;

						case R.id.fr_rb_remote:
							mViewFlipper.setDisplayedChild(0);
							break;
					}
				}
			});
			if (savedInstanceState == null) {
				mRadioGroup.check(R.id.fr_rb_remote);
			} else {
				mRadioGroup.check(savedInstanceState.getInt(FR_MYSTNIHON_REMOTE_CHECKED_GENERAL_BUTTON));
			}
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (mViewFlipper != null && mRadioGroup != null) {
			outState.putInt(FR_MYSTNIHON_REMOTE_CHECKED_GENERAL_BUTTON, mRadioGroup.getCheckedRadioButtonId());
		}
	}

	// @Override
	// public void onActivityCreated(Bundle savedInstanceState) {
	// super.onActivityCreated(savedInstanceState);
	// FragmentTransaction ft = getChildFragmentManager().beginTransaction();
	// ft.add(R.id.fr_fl_remote_layout, RemoteFragment.newInstance());
	// ft.commit();
	// }
}
